﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DemoServer
{
    class Program
    {
        static Shared.Method.ServerTcp serverTcp = new Shared.Method.ServerTcp(20, 0x200000);
        static void Main(string[] args)
        {
            serverTcp.Init();

            serverTcp.SetPacketOn();
            serverTcp.OnAcceptHandler += ServerTcp_OnAcceptHandler;
            serverTcp.OnCloseHandler += ServerTcp_OnCloseHandler;
            serverTcp.OnErrorHandler += ServerTcp_OnErrorHandler;
            serverTcp.OnReceiveHandler += ServerTcp_OnReceiveHandler;
            serverTcp.OnSendHandler += ServerTcp_OnSendHandler;

            serverTcp.Start(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 32110));

        }

        private static void ServerTcp_OnSendHandler(string message)
        {
            Console.WriteLine(message);
        }

        private static void ServerTcp_OnReceiveHandler(System.Net.Sockets.Socket recvSocket, byte[] recvBytes, int offset, int length)
        {
            //throw new NotImplementedException();
            string recvMsg = Encoding.UTF8.GetString(recvBytes, offset, length);
            Console.WriteLine($"Recv msg:{recvMsg}");
            serverTcp.SendBytes(recvSocket, Encoding.UTF8.GetBytes("MEG oooQQ你好吗"));
        }

        private static void ServerTcp_OnErrorHandler(string message)
        {
            Console.WriteLine(message);
        }

        private static void ServerTcp_OnCloseHandler(string message)
        {
            Console.WriteLine(message);
        }

        private static void ServerTcp_OnAcceptHandler(System.Net.Sockets.Socket sock, string message)
        {
            Console.WriteLine(message);
            Console.WriteLine($"Client's IP [{sock.RemoteEndPoint}]");
        }
    }
}
